/*
 * Configurator.java
 * Copyright (C) 2007 Ezequiel Turovetzky
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
package ar.com.botonera.config;

import java.io.File;

import ar.com.botonera.Application;
import ar.com.botonera.Constants;
import ar.com.botonera.utils.FileUtils;

/**
 * Clase que contiene una referencia a la &uacute;nica instancia de la
 * configuraci&oacute;n de la aplicaci&oacute;n.
 * <br>
 * @author <a href="mailto:tezequiel@gmail.com">Ezequiel Turovetzky</a>
 * @version 1.0
 */
public class Configurator {

    //~ Static fields/initializers ---------------------------------------------

    /** La unica instancia de esta clase */
    private static Configurator instance;
    /** La configuracion */
    private Configuration configuration;

    //~ Constructors -----------------------------------------------------------

    /**
     * Crea una nueva instancia de esta clase.
     */
    private Configurator() {
        File file = new File(Constants.USER_CONFIG_FILE);
        if(!file.exists()) {
        	try {
        		FileUtils.createDirectory(Constants.USER_CONFIG_DIR);
        		String str = Configurator.class.getClassLoader().getResource(Constants.DEFAULT_CONFIG_FILE).getFile();
        		FileUtils.copyFile(str, Constants.USER_CONFIG_FILE);
        	} catch(Exception e) {
        		Application.getLogger().fatal("Error al crear archivo de configuracion del usuario: " + e.getMessage(), e);
        		Application.getInstance().quit();
        	}
        }
        configuration = new Configuration(file);
    }

    //~ Methods ----------------------------------------------------------------

    /**
     * Devuelve la &uacute;nica instancia de esta clase.
     * @return La &uacute;nica instancia de esta clase.
     */
    public static Configurator getInstance() {
        if(instance == null) {
            instance = new Configurator();
        }
        return instance;
    }

    /**
     * Devuelve la configuraci&oacute;n de la aplicaci&oacute;n..
     * @return La configuraci&oacute;n de la aplicaci&oacute;n..
     */
    public Configuration getConfiguration() {
        return configuration;
    }
}
